package dev.alm.new_service.controller;

import dev.alm.new_service.common.CommonResult;
import dev.alm.new_service.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {

    @PostMapping("removeUserById")
    public CommonResult removeUserById(@RequestBody User user) {
        CommonResult cr = new CommonResult();
        Integer id = user.getId();
        if (id == null) {
            cr.setCode("400");
            cr.setMessage("参数错误");
            return cr;
        }
        if (id == 1 || id == 2 || id == 3) {
            cr.setCode("200");
            cr.setMessage("删除成功");
            return cr;
        } else {
            cr.setCode("400");
            cr.setMessage("用户不存在");
            return cr;
        }
    }

    @PostMapping("getUserListByCondition")
    public CommonResult getUserListByCondition(@RequestBody User user) {

        ArrayList<User> userList = new ArrayList<>();
        User user1 = new User();
        user1.setId(1);
        user1.setUsername("zhangsan");
        user1.setPassword("abc123");
        user1.setEmail("zhangsan@qq.com");
        user1.setPhone("12345678901");
        user1.setAddress("上海");
        user1.setStatus("1");
        userList.add(user1);

        User user2 = new User();
        user2.setId(2);
        user2.setUsername("lisi");
        user2.setPassword("abc123");
        user2.setEmail("lisi@qq.com");
        user2.setPhone("12345678902");
        user2.setAddress("北京");
        user2.setStatus("0");
        userList.add(user2);

        User user3 = new User();
        user3.setId(3);
        user3.setUsername("wangwu");
        user3.setPassword("abc123");
        user3.setEmail("wangwu@qq.com");
        user3.setPhone("12345678903");
        user3.setAddress("广州");
        user3.setStatus("1");
        userList.add(user3);

        ArrayList<User> retList = new ArrayList<>();

        String status = user.getStatus();
        if (status == null || status.equals("")) {
            retList = userList;
        } else {
            for (User u : userList) {
                if (u.getStatus().equals(status)) {
                    retList.add(u);
                }
            }
        }

        CommonResult cr = new CommonResult();
        cr.setCode("200");
        cr.setMessage("查询成功");
        cr.setData(retList);
        return cr;
    }
}
